<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/25
 * Time: 19:57
 */

namespace App\Process;


use App\Spider\GameMatchEvent;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\MysqliPool\Mysql;
use EasySwoole\RedisPool\Redis;
use Swoole\Coroutine;

class ConsumeGameMatchs extends AbstractProcess
{
    protected function run($arg)
    {
        $consumeListKey = 'easyswooleConsumeList';
        $maxDepth = $arg['maxDepth'];
        $maxCoroutineNum = $arg['maxCoroutineNum'] ?? 3; //最大携程数量
        for ( $i = 0;$i<$maxCoroutineNum;$i++ ){
            go(function ()use($consumeListKey){
                Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($consumeListKey){
                    while (1){
                        $data = $redis->lPop($consumeListKey);
                        if( empty($data) ){
                            Logger::getInstance()->console('暂无消费队列');
                            Coroutine::sleep(5);
                            continue;
                        }
                        $res = GameMatchEvent::consume($data);
                        if( $res ){
                            Logger::getInstance()->console('消费完成');
                        }
                    }
                });
            });
        }
    }
}